// pages/myeventlist/myeventlist.js
var util = require("../../utils/util.js")
const db = wx.cloud.database()
const event = db.collection('event')
const user = db.collection('user')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    eventlist:[],
    eventidlist:[],
    waitinglist:[],
    unvaluelist:[],
    finishlist:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var myeventidlist=[]
    var templist=[]
    var _this = this
    _this.setData({
      active: options.active
    })
    user.field({
      myevents: true,
    })
    .get()
    .then(res=>{
      myeventidlist = res.data[0].myevents
      _this.data.eventidlist=myeventidlist
      for (var i=0; i < myeventidlist.length;i++){
        event
        .doc(myeventidlist[i].event_id)
        .get()
        .then(res=>{
          for (var j = 0; j < myeventidlist.length;j++){
            if (res.data._id == myeventidlist[j].event_id){
              res.data.score = myeventidlist[j].score
              res.data.fmttimefrom = util.formatTime(res.data.timefrom)
              templist.push(res.data)
            }
          }
          _this.setData({
            eventlist: templist
          },()=>{
            // 填充待参加列表数组
            var temp = []
            var timestamp = new Date()
            for (var i = 0; i < _this.data.eventlist.length; i++) {
              if (_this.data.eventlist[i].timefrom > timestamp) {
                temp.push(_this.data.eventlist[i])
              }
              _this.setData({
                waitinglist: temp
              })
            }
            // 填充待评价列表数组
            var temp1 = []
            var timestamp = new Date()
            for (var i = 0; i < _this.data.eventlist.length; i++) {
              if (_this.data.eventlist[i].score == 0 && _this.data.eventlist[i].timefrom < timestamp) {
                temp1.push(_this.data.eventlist[i])
              }
              _this.setData({
                unvaluelist: temp1
              })
            }
            // 填充已结束列表数组
            var temp2 = []
            var timestamp = new Date()
            for (var i = 0; i < _this.data.eventlist.length; i++) {
              if (_this.data.eventlist[i].score != 0 && _this.data.eventlist[i].timefrom < timestamp) {
                temp2.push(_this.data.eventlist[i])
              }
              _this.setData({
                finishlist: temp2
              })
            }
          })
        })
      }
    })
  },

  Gotoeventdetail: function (event) {
    wx.navigateTo({
      url: '/pages/eventdetail/eventdetail?id=' + event.currentTarget.id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // onChange(event) {
  //   wx.showToast({
  //     title: `切换到标签 ${event.detail.index + 1}`,
  //     icon: 'none'
  //   });
  // }

})